<?php
session_start();

define('EIP_START',microtime(true));
require_once('bootstrap.php');

$logger=E::get_logger();
$logger->debug("Request URI: {$_SERVER['REQUEST_URI']}");

$requestComponents = preg_split('/\?/i', $_SERVER['REQUEST_URI'],-1,PREG_SPLIT_NO_EMPTY); 
define('REQ_URL',$requestComponents[0]);
if(count($requestComponents)>1)
	define('QUERY',$requestComponents[1]);

$requestComponents = preg_split('|/|', $requestComponents[0],-1,PREG_SPLIT_NO_EMPTY);


$controllerName		= 'main';
$action			= 'index';


$offset = count($requestComponents)-1;

if( count($requestComponents) > 2)
{
	$controllerNamePrefix = '';
	for($x=0; $x<count($requestComponents)-2;$x++)
		$controllerNamePrefix.=$requestComponents[$x].'/';
}

//we assume the the first part of url is the controller.. the next is the action
if($offset  && isset($requestComponents[$offset-1]) && strlen($requestComponents[$offset]))
	$controllerName = $requestComponents[$offset-1];
else if (isset($requestComponents[0]))
	$controllerName = $requestComponents[0];



if($offset && isset($requestComponents[$offset]) && strlen($requestComponents[$offset]))
	$action = $requestComponents[$offset];


if(isset($controllerNamePrefix))
	$controllerName = $controllerNamePrefix . $controllerName;

//fix name of calling functions
$controllerName	= preg_replace('/(\b|_)([a-z])/e','"$1".strtoupper("$2")', $controllerName); //
$action	= preg_replace('/(\b|_)([a-z])/e','"$1".strtoupper("$2")', $action); //fix capitals
$action = preg_replace('/(.*?)\.([cC]ss|[jJ]s)/e','"$1.".strtolower("$2")', $action); //fix js/css


//was this request made from js?
define('AJAX', (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? true : false );

//$controllerName is should always include the path and controller name, we alias to short name in EIP autoloader
$full_name = E::getFullControllerClassName($controllerName,$action);
$logger->debug("Controller Name Final: {$full_name}");

$controller = null;
if($full_name==null)
{
	if(  preg_match('#(.*?)\.([a-zA-Z0-9]{1,3})$#', $controllerName) ) {
		throw new ExceptionFileNotFound(sprintf("The requested file: '%s' was not found.", $controllerName) );
     } 
	$controller = new EIP\C\Controller($controllerName);
} else 
	$controller = new $full_name($full_name,$action);
$controller->logger->info("Started Action:'$action'");

//check for redirected action. This allows controllers constructers to move action
define('ACTION', (defined('REDIRECT_ACTION')) ? REDIRECT_ACTION : $action);
//update action
$controller->action = $action;

$ret = $controller->$action();

//assume with disable output that we have json data
if ($controller->disable_output)
{
	//we must respond with ajax data
	header('Cache-Control: no-cache, must-revalidate');
	header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
	header('Content-type: application/json');
	print json_encode(array($ret));
}

$controller->__render();
$controller->logger->info("Finished Action: '{ACTION}'");
$logger->debug("Execution Done");
?>
